package com.yonyou.brigade.rtpnr.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yonyou.brigade.basic_init.enums.ChannelEnum;
import com.yonyou.brigade.basic_init.model.BriChannel;
import com.yonyou.brigade.basic_init.service.IBriChannelService;
import com.yonyou.brigade.rtpnr.core.CXFInvokeClient;
import com.yonyou.brigade.rtpnr.model.*;
import com.yonyou.brigade.rtpnr.service.IBriRtpnrpriService;
import com.yonyou.brigade.rtpnr.service.IRtpnrOrderService;
import com.yonyou.brigade.rtpnr.service.IRtpnrPublicService;
import com.yonyou.brigade.rtpnr.service.IRtpnrUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/test")
public class TestRtpnrController {

    @Autowired
    @Qualifier("rtpnrUserServiceImpl")
    private IRtpnrUserService userService;


    @Qualifier("rtpnrPublicServiceImpl")
    @Autowired
    private IRtpnrPublicService iRtpnrPublicService;

    @Qualifier("rtpnrOrderServiceImpl")
    @Autowired
    private IRtpnrOrderService rtpnrOrderService;

    @Autowired
    @Qualifier("briChannelServiceImpl")
    private IBriChannelService briChannelService;

    @Qualifier("briRtpnrpriServiceImpl")
    @Autowired
    private IBriRtpnrpriService iBriRtpnrpriService;

    @Autowired
    CXFInvokeClient cxfInvokeClient;


    String tenant="uq0dskd6";
    String org="0001A2100000000045GK";

    @RequestMapping(value = "/asyncuser",method = RequestMethod.GET)
    public JSONObject testCreateAccount(){
        String tenant="ft9fbcrw";
        String phone="13021171165";
        String orgpk="0001A210000000003ADC";
        userService.syncRtpnrSingleUserByPhone(tenant,orgpk,phone,false);
        return  new JSONObject();
    }

    @RequestMapping(value = "/asyncusers",method = RequestMethod.GET)
    public JSONObject testSyncusersBytenantAndOrg(){
        String tenant="es0jmgsl";
        String orgpk="0001F410000000000P18";
        Map map = userService.syncRtpnrUsers(tenant,orgpk);
        return (JSONObject) JSON.toJSON(map);
    }

    @RequestMapping(value = "/createAccount",method = RequestMethod.POST)
    public JSONObject testCreateAccount(@RequestBody  JSONObject body){
        String userName = body.getString("userName");
        String mobile = body.getString("mobile");

        RtpnrUser user=new RtpnrUser();
        user.setUserName(userName);
        user.setMobile(mobile);
          JSONObject jsonObject = iRtpnrPublicService.createAccountInfo(tenant,org, user);
        return jsonObject;
    }

    @RequestMapping(value = "/ssoLogin",method = RequestMethod.POST)
    public JSONObject testLogin(@RequestBody JSONObject body){
        JudgeAccountByLoginID judgeAccountByLoginID =
                JSONObject.parseObject(body.toJSONString(),JudgeAccountByLoginID.class);
        JSONObject jsonObject = iRtpnrPublicService.ssoLogin(tenant,org,judgeAccountByLoginID);
        return jsonObject;
    }

    @RequestMapping(value = "/getFlightOrderList",method = RequestMethod.POST)
    public JSONObject testGetFlightOrderList( @RequestBody GetFlightOrderList getFlightOrderList){
        JSONObject jsonObject = iRtpnrPublicService.getFlightOrderList(tenant,org,getFlightOrderList);
        return jsonObject;
    }

    @RequestMapping(value = "/getFlightOrderDetail",method = RequestMethod.POST)
    public JSONObject testGetFlightOrderDetail(@RequestBody JSONObject body){
        GetFlightOrderDetail getFlightOrderDetail =
                JSONObject.parseObject(body.toJSONString(),GetFlightOrderDetail.class);
        JSONObject jsonObject = iRtpnrPublicService.getFlightOrderDetail(tenant,org,getFlightOrderDetail);
        return jsonObject;
    }

    @RequestMapping(value = "/getHotelOrderDetail",method = RequestMethod.POST)
    public JSONObject testGetHotelOrderDetail(@RequestBody JSONObject body){
        GetHotelOrderDetail getHotelOrderDetail =
                JSONObject.parseObject(body.toJSONString(),GetHotelOrderDetail.class);
        JSONObject jsonObject = iRtpnrPublicService.getHotelOrderDetail(tenant,org,getHotelOrderDetail);
        return jsonObject;
    }

    @RequestMapping(value = "/getCarOrderList",method = RequestMethod.POST)
    public JSONObject testGetCarOrderList(@RequestBody JSONObject body, String tenant, String org){
        GetCarOrderList getCarOrderList = JSONObject.parseObject(body.toJSONString(), GetCarOrderList.class);
        JSONObject jsonObject = iRtpnrPublicService.getCarOrderList(tenant, org, getCarOrderList);
        return jsonObject;
    }


    @RequestMapping(value = "/getCarOrderDetail",method = RequestMethod.POST)
    public JSONObject testGetCarOrderDetail( @RequestBody JSONObject body, String tenant, String org){
        GetCarOrderDetail getCarOrderDetail = JSONObject.parseObject(body.toJSONString(), GetCarOrderDetail.class);
        JSONObject jsonObject = iRtpnrPublicService.getCarOrderDetail(tenant, org, getCarOrderDetail);
        return jsonObject;
    }

    @RequestMapping(value = "/pullCarOrders",method = RequestMethod.POST)
    public String testPullCarOrders( @RequestBody JSONObject body, String tenant, String org){
        GetCarOrderList getCarOrderList = JSONObject.parseObject(body.toJSONString(), GetCarOrderList.class);
        BriChannel channel = briChannelService.getEnableChannelByPersonTenantOrgAndChannel(tenant, org, ChannelEnum.RTPNR.getName());

        rtpnrOrderService.getCarOrderList(tenant, org, channel.getId(),getCarOrderList.getStartDt(),getCarOrderList.getEndDt());
        return "success";
    }

    @RequestMapping(value = "/getTrainOrderList",method = RequestMethod.POST)
    public JSONObject testGetTrainOrderList(@RequestBody JSONObject body){
        GetTrainOrderList getTrainOrderList =
                JSONObject.parseObject(body.toJSONString(),GetTrainOrderList.class);
        JSONObject jsonObject = iRtpnrPublicService.getTrainOrderList(tenant,org,getTrainOrderList);
        return jsonObject;
    }


    @RequestMapping(value = "/getTravelCodeList",method = RequestMethod.POST)
    public JSONObject testGetTravelCodeList( @RequestBody GetTravelCodeList getTravelCodeList){
        JSONObject jsonObject = iRtpnrPublicService.getTravelCodeList(tenant,org,getTravelCodeList);
        return jsonObject;
    }

    @RequestMapping(value = "/getAdmdivCodeList",method = RequestMethod.POST)
    public JSONObject testGetAdmdivCodeList( @RequestBody GetAdmdivCodeList getAdmdivCodeList){
        JSONObject jsonObject = iRtpnrPublicService.getAdmdivCodeList(tenant,org,getAdmdivCodeList);
        return jsonObject;
    }

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String testLogin( @RequestBody Map<String, String> params){
        String tenant = params.get("tenant");
        String org = params.get("org");
        String phone = params.get("phone");
        String jsonObject = userService.loginUrl(tenant, org, phone,  "main", "h5", "");
        return jsonObject;
    }


    @RequestMapping(value = "/AddRtpnrPriURL",method = RequestMethod.POST)
    public Object testAddRtpnrPriURL( @RequestBody JSONObject body){

        String tenant=body.getString("tenant");
        String orgpk=body.getString("orgpk");
        String rtpnrPriURL=body.getString("rtpnrPriURL");
        String tmcid = body.getString("tmcid");
        String rt = cxfInvokeClient.addRtpnrClien(tenant,orgpk,rtpnrPriURL,tmcid);
        Map<String ,Object> result=new HashMap<>();
        if(rt != null){
            result.put("code","0");
            result.put("information","操作成功");
        }else {
            result.put("code","1");
            result.put("information","操作失败");
        }
        return result;
    }

}
